import { NSwitch } from "naive-ui";
import type { HTMLAttributes } from "vue";
import type { SwitchProps } from "naive-ui";
import { getSlotsDom } from "../utils";
import type { BaseComponentType } from "../types";
export type SwitchFactoryConstructorType = {
  props?: SwitchProps;
  attrs?: HTMLAttributes;
  checked?: BaseComponentType;
  checkedIcon?: BaseComponentType;
  icon?: BaseComponentType;
  unchecked?: BaseComponentType;
  uncheckedIcon?: BaseComponentType;
};
export class SwitchFactory {
  private attrs: HTMLAttributes = {};
  private props: SwitchProps = {};
  private checkedSlot: BaseComponentType = null;
  private checkedIconSlot: BaseComponentType = null;
  private iconSlot: BaseComponentType = null;
  private uncheckedSlot: BaseComponentType = null;
  private uncheckedIconSlot: BaseComponentType = null;

  constructor(data?: SwitchFactoryConstructorType) {
    if (data?.props) this.setProps(data.props);
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.checked) this.setChecked(data.checked);
    if (data?.checkedIcon) this.setCheckedIcon(data.checkedIcon);
    if (data?.icon) this.setIcon(data.icon);
    if (data?.unchecked) this.setUnchecked(data.unchecked);
    if (data?.uncheckedIcon) this.setUncheckedIcon(data.uncheckedIcon);
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }
  setProps(props: SwitchProps) {
    this.props = props;
    return this;
  }
  setChecked(component: BaseComponentType) {
    this.checkedSlot = component;
    return this;
  }

  setCheckedIcon(component: BaseComponentType) {
    this.checkedIconSlot = component;
    return this;
  }

  setIcon(component: BaseComponentType) {
    this.iconSlot = component;
    return this;
  }

  setUnchecked(component: BaseComponentType) {
    this.uncheckedSlot = component;
    return this;
  }

  setUncheckedIcon(component: BaseComponentType) {
    this.uncheckedIconSlot = component;
    return this;
  }

  create() {
    return (
      <NSwitch {...this.attrs} {...this.props}>
        {{
          checked: () => getSlotsDom(this.checkedSlot),
          "checked-icon": () => getSlotsDom(this.checkedIconSlot),
          icon: () => getSlotsDom(this.iconSlot),
          unchecked: () => getSlotsDom(this.uncheckedSlot),
          "unchecked-icon": () => getSlotsDom(this.uncheckedIconSlot),
        }}
      </NSwitch>
    );
  }
}
